package com.example.lin.guidancedemo;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.lin.guidancedemo.adpater.GuidancePagerAdpater;
public class GuidanceActivity extends AppCompatActivity {

    private static final String TAG = "GuidanceActivity";
    private ViewPager vp;
    private RadioGroup rg_indicator;
    private View view_indicator;
    //屏幕宽度
    private int widthPixels;
    //指示器的宽度
    private int indicatorWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guidance);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        widthPixels = metrics.widthPixels;
        indicatorWidth = widthPixels / 5;
        initView();
        initLinstener();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        rg_indicator = findViewById(R.id.rg_indicator);
        vp = findViewById(R.id.vp);
        view_indicator = findViewById(R.id.view_indicator);
        //改变指示器的宽度为 屏幕的 1/5
        //step1:获取 view_indicator 的布局参数
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view_indicator.getLayoutParams();
        //step2:修改布局参数
        layoutParams.width = indicatorWidth;
        //step3:更新 view_indicator 的布局参数
        view_indicator.setLayoutParams(layoutParams);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        int[] ints = new int[]{R.mipmap.g1, R.mipmap.g2, R.mipmap.g3, R.mipmap.g4, R.mipmap.g5};
        GuidancePagerAdpater guidancePagerAdpater = new GuidancePagerAdpater(ints, this);
        vp.setAdapter(guidancePagerAdpater);
    }

    private void initLinstener() {
        //ViewPager 页面改变的监听事件
        vp.addOnPageChangeListener(l);

        //监听RadioButton的点击事件,切换ViewPager需要显示的页面
//        rg_indicator.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                switch (checkedId){
//                    case R.id.rb1:
//                        vp.setCurrentItem(0);
//                        break;
//                    case R.id.rb2:
//                        vp.setCurrentItem(1);
//                        break;
//                }
//            }
//        });
    }


    private ViewPager.OnPageChangeListener l = new ViewPager.OnPageChangeListener() {
        /**
         * 页面正在滚动中
         * @param position 最左边的页面的下标
         * @param positionOffset 页面移动的百分比
         * @param positionOffsetPixels 页面移动的像素点
         */
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                Log.d(TAG, "onPageScrolled: " + position + ">>>" + positionOffset + ">>>" + positionOffsetPixels);
            view_indicator.setX((position + positionOffset) * indicatorWidth);
            if (positionOffset > 0.5f) {
                view_indicator.setScaleX(3 - positionOffset * 2);
            } else {
                view_indicator.setScaleX(1 + positionOffset * 2);
            }
        }

        /**
         * 当滑动结束的时候,该方法被调用
         * @param position 当前显示的页面的 下标
         */
        @Override
        public void onPageSelected(int position) {
//                Log.w(TAG, "onPageSelected: " + position);
            //获取RadioGroup的子View

            //改变 RadioButton的选中状态
            RadioButton childAt = (RadioButton) rg_indicator.getChildAt(position);
            childAt.setChecked(true);
        }

        /**
         * 页面滚动状态"改变"的时候,该方法被调用
         * @see ViewPager#SCROLL_STATE_IDLE
         * @see ViewPager#SCROLL_STATE_DRAGGING
         * @see ViewPager#SCROLL_STATE_SETTLING
         * @param state 滑动的状态
         */
        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case ViewPager.SCROLL_STATE_IDLE://页面停止的时候
                    Log.i(TAG, "onPageScrollStateChanged: SCROLL_STATE_IDLE");
                    break;
                case ViewPager.SCROLL_STATE_DRAGGING://正在拖拽的时候
                    Log.i(TAG, "onPageScrollStateChanged: SCROLL_STATE_DRAGGING");
                    break;
                case ViewPager.SCROLL_STATE_SETTLING://惯性滑动的时候
                    Log.i(TAG, "onPageScrollStateChanged: SCROLL_STATE_SETTLING");
                    break;
            }
        }
    };
}
